package com.capgemini.scaffold.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.capgemini.scaffold.utils.UserHolder;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class DateMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "update_time", LocalDateTime.class, LocalDateTime.now());
       //setFieldValByName(fieldName:"createTime", new Date(),  metaObject)
        this.strictInsertFill(metaObject, "founders", String.class, UserHolder.getUser().getName());
        this.strictUpdateFill(metaObject, "modifier", String.class, UserHolder.getUser().getName());
    }
    //设置数据修改update时候的，字段自动赋值规则
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "update_time", LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, "modifier", String.class, UserHolder.getUser().getName());
    }
//lingpai.getUsernameFromToken())令牌
}
